package so.shanku.essential.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.searchUtils.SortModel;

public class BrandSearchAdapter extends BaseAdapter implements SectionIndexer {
    private ItemAttentionChange attentionChange;
    private List<SortModel> list = null;
    private Context mContext;

    public List<SortModel> getList() {
        return list;
    }

    public void setList(List<SortModel> list) {
        this.list = list;
    }

    public BrandSearchAdapter(Context mContext,List<SortModel> list, ItemAttentionChange callback2) {
        this.mContext = mContext;
        this.list = list;
        this.attentionChange = callback2;
    }
    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<SortModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        final SortModel mContent = list.get(position);
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_manbrand_hot, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
//		if(position == getPositionForSection(section)){
//			viewHolder.tvLetter.setVisibility(View.VISIBLE);
//			viewHolder.tvLetter.setText(mContent.getSortLetters());
//		}else{
//			viewHolder.tvLetter.setVisibility(View.GONE);
//		}

        String vendorId =this.list.get(position).getId();
        boolean isSelected = MyApplication.getInstance().validateAttendBrands(vendorId);
        viewHolder.tv_if_attention.setSelected(isSelected);
        if (isSelected) {
            viewHolder.tv_if_attention.setText(R.string.attentioned);
        } else {
            viewHolder.tv_if_attention.setText(R.string.attention);
        }

        viewHolder.tv_name.setText(this.list.get(position).getName());
        viewHolder.tv_msg.setText(this.list.get(position).getTv_msg());
        String headlog =this.list.get(position).getIv_log_url();
        if (MyApplication.getInstance().getBitmapHashMap().containsKey(headlog)) {
            viewHolder.iv_head.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(headlog)));
        } else {
            ImageLoader.getInstance().displayImage(headlog,viewHolder.iv_head, MyApplication.getInstance().getBrandDefaultOption(), new ImageLoaderBitmapCallBack());
        }

        viewHolder.tv_if_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (attentionChange != null) {
                    attentionChange.onStatusChange(position, viewHolder.tv_if_attention);
                }
            }
        });
        return view;

    }


    static class ViewHolder {
        @ViewInject(R.id.iv_head)
        private ImageView iv_head;
        @ViewInject(R.id.tv_name)
        private TextView tv_name;
        @ViewInject(R.id.tv_msg)
        private TextView tv_msg;
        @ViewInject(R.id.tv_if_attention)
        private TextView tv_if_attention;

        public ViewHolder(View view) {
            ViewUtils.inject(this, view);
        }
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
    public interface ItemAttentionChange{
        void onStatusChange(int position,TextView tv_if_attention);
    }
}